package com.zzgc.modules.service.impl;

import com.zzgc.common.dao.SchoolDao;
import com.zzgc.common.entity.School;
import com.zzgc.modules.service.SchoolService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 学校(School)表服务实现类
 *
 * @author makejava
 * @since 2022-11-19 09:40:41
 */
@Service("schoolService")
public class SchoolServiceImpl implements SchoolService {
    @Resource
    private SchoolDao schoolDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public School queryById(Long id) {
        return this.schoolDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param school 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public List<School> queryByPage(School school) {
        List<School> schools = this.schoolDao.queryAllByLimit(school);
        long total = this.schoolDao.count(school);
        if (total == 0){
            return new ArrayList<>();
        }
        schools.get(0).setTotal(total);
        return schools;
    }

    /**
     * 新增数据
     *
     * @param school 实例对象
     * @return 实例对象
     */
    @Override
    public School insert(School school) {
        this.schoolDao.insert(school);
        return school;
    }

    /**
     * 修改数据
     *
     * @param school 实例对象
     * @return 实例对象
     */
    @Override
    public School update(School school) {
        this.schoolDao.update(school);
        return this.queryById(school.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.schoolDao.deleteById(id) > 0;
    }
}
